package com.rybbaby.tss.front.tssFeedback.controller;

import com.qiniu.util.Auth;
import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.consts.RoleConsts;
import com.rybbaby.tss.core.hqlsearch.HqlGenerateUtil;
import com.rybbaby.tss.core.utils.*;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssCourse.entity.TssCourse;
import com.rybbaby.tss.front.tssFeedback.entity.TssFeedback;
import com.rybbaby.tss.front.tssFeedback.service.ITssFeedbackService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.platform.initializer.SysConfigInitializer;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import com.rybbaby.tss.platform.sysAdminUser.service.ISysAdminUserService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 反馈意见
 * @createDate 2017-08-09 下午11:56:55
 */
@Controller
@RequestMapping(value = "TssFeedbackController")
public class TssFeedbackController extends BaseController {

    @Autowired
    private ITssFeedbackService tssFeedbackService;
    @Autowired
    private ISysAdminUserService sysAdminUserService;

    /**
     * 转到意见反馈
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "转到意见反馈" , save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "TssFeedback" , method = RequestMethod.GET)
    public ModelAndView TssFeedback(TssFeedback entity , PageBean paramPageBean) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("TssFeedbackView"));
        String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
        mav.addObject("saveUrl", requestURI + "/toEditView.do");
        return mav;
    }
    /**
     * 意见反馈列表
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "意见反馈列表", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "TssFeedbackList", method = RequestMethod.POST)
    public ModelAndView TssFeedbackList(TssFeedback entity, PageBean paramPageBean) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("TssFeedbackListView"));
        try {
            Map<String, String[]> unLockMap = getParameterMap(request);
            DetachedCriteria dc = DetachedCriteria.forClass(TssFeedback.class);
            HqlGenerateUtil.fillDetachedCriteria(dc, entity, unLockMap);

            String ids = "";
            //获取当前登录者
            String adminUserId = super.getLoginUser().getId();
            SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
            //获取当前登录者所拥有角色
            List<String> roleList = super.getLoginAdminRoles();
            if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
                dc.add(Restrictions.sqlRestriction(" createUserId in ('" + adminUserId + "') "));
            }else if(roleList.contains(RoleConsts.TSS_ROLE_KINDERGARTEN_LEADER)
                    ||roleList.contains(RoleConsts.TSS_COUNSELOR_DIRECTOR)
                    ||roleList.contains(RoleConsts.TSS_TEACHING_AFFAIRS)
                    ||roleList.contains(RoleConsts.TSS_FINANCE)
                    ||roleList.contains(RoleConsts.TSS_TEACHING_DIRECTOR)){
                List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
                ids = StringUtils.join(idsList, "','");
                dc.add(Restrictions.sqlRestriction(" createUserId in ('" + ids + "') "));

            }else{

            }

            PageBean pageBean = tssFeedbackService.pageList(dc, paramPageBean.getPageNo(), paramPageBean.getPageSize());

            mav.addObject(super.SEARCH_MAP, unLockMap);
            mav.addObject(super.PAGE_BEAN, pageBean);
            mav.addObject("searchFormId", SEARCH_FORM);
            mav.addObject("actionUrl", request.getRequestURI());
            mav.addObject("pagingBar", pageBean.getPagingBar(SEARCH_FORM));
            mav.addObject("serializeNum", SequenceUtil.getUpperCaseUUID());
            String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
            mav.addObject("editUrl", requestURI + "/toEditView.do");
            mav.addObject("deleteUrl", requestURI + "/delete.do");
            mav.addObject("batchDeleteUrl", requestURI + "/batchDelete.do");
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }

    /**
     * 意见反馈详情
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "意见反馈详情", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "tssFeedbackDetails", method = RequestMethod.GET)
    public ModelAndView tssFeedbackDetails(TssFeedback entity) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("TssFeedbackDetailsView"));
        try {
            Map<String, String[]> unLockMap = getParameterMap(request);
            DetachedCriteria dc = DetachedCriteria.forClass(TssFeedback.class);
            HqlGenerateUtil.fillDetachedCriteria(dc, entity, unLockMap);

            TssFeedback tssFeedback = tssFeedbackService.get(entity.getId());

            //PageBean pageBean = tssFeedbackService.pageList(dc, paramPageBean.getPageNo(), paramPageBean.getPageSize());

            mav.addObject("tssFeedback", tssFeedback);
            mav.addObject("searchFormId", SEARCH_FORM);
            mav.addObject("actionUrl", request.getRequestURI());
            mav.addObject("serializeNum", SequenceUtil.getUpperCaseUUID());
            String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
            mav.addObject("editUrl", requestURI + "/toEditView.do");
            mav.addObject("deleteUrl", requestURI + "/delete.do");
            mav.addObject("batchDeleteUrl", requestURI + "/batchDelete.do");
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }

    /**
     * 提交意见
     *
     * @param entity
     *            被编辑对象
     * @return
     */
    @ResponseBody
    @RequestLog(content = "提交意见" , save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "comments" , method = RequestMethod.GET)
    public String comments(TssFeedback entity,@RequestParam(required = true) String title
            ,@RequestParam(required = true) String content,@RequestParam(required = true) Integer phone) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        String adminUserId = super.getLoginUser().getId();
        try {
            ajr.setResMsg("保存成功!");
            entity.setCreateUserId(adminUserId);
            entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            entity.setUpdateTIme(new java.sql.Timestamp(System.currentTimeMillis()));
            Serializable saveId = tssFeedbackService.saveFeedback(entity);
            if(saveId == null){
                ajr.setResMsg("保存失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }

    /**
     * 意见反馈处理界面
     *
     * @return
     * @throws IOException
     */
    @RequestLog(content = "意见反馈处理界面", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "processTssFeedbackDetails", method = RequestMethod.GET)
    public ModelAndView processTssFeedbackDetails(TssFeedback entity) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ProcessTssFeedbackDetailsView"));
        try {
            Map<String, String[]> unLockMap = getParameterMap(request);
            DetachedCriteria dc = DetachedCriteria.forClass(TssFeedback.class);
            HqlGenerateUtil.fillDetachedCriteria(dc, entity, unLockMap);

            TssFeedback tssFeedback = tssFeedbackService.get(entity.getId());

            //PageBean pageBean = tssFeedbackService.pageList(dc, paramPageBean.getPageNo(), paramPageBean.getPageSize());

            mav.addObject("tssFeedback", tssFeedback);
            mav.addObject("searchFormId", SEARCH_FORM);
            mav.addObject("actionUrl", request.getRequestURI());
            mav.addObject("serializeNum", SequenceUtil.getUpperCaseUUID());
            String requestURI = StringUtils.substringBeforeLast(request.getRequestURI(), "/");
            mav.addObject("editUrl", requestURI + "/toEditView.do");
            mav.addObject("deleteUrl", requestURI + "/delete.do");
            mav.addObject("batchDeleteUrl", requestURI + "/batchDelete.do");
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }
    /**
     * 处理反馈
     *
     * @param entity
     *            被编辑对象
     * @return
     */
    @ResponseBody
    @RequestLog(content = "处理反馈" , save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "processingComments" , method = RequestMethod.GET)
    public String processingComments(TssFeedback entity,@RequestParam(required = true) String id,@RequestParam(required = true) String processingContent) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        String adminUserId = super.getLoginUser().getId();
        try {
            ajr.setResMsg("保存成功!");
            TssFeedback tssFeedback = tssFeedbackService.get(entity.getId());

            tssFeedback.setStatus(1);
            tssFeedback.setProcessingContent(entity.getProcessingContent());
            tssFeedback.setUpdateUserId(adminUserId);
            tssFeedback.setUpdateTIme(new java.sql.Timestamp(System.currentTimeMillis()));

            tssFeedbackService.updateEntitiy(tssFeedback);
        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }

}
